package com.corechina.apex_osswords.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.ShiroUtils;
import com.corechina.apex_osswords.common.SysLog;
import com.corechina.apex_osswords.domain.entity.CoreProjectType;
import com.corechina.apex_osswords.service.ICoreProjectTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 项目分类表 前端控制器
 * </p>
 *
 * @author cc
 * @since 2022-03-11
 */
@Api(tags = "项目分类")
@RestController
@RequestMapping("/core/projectType")
public class CoreProjectTypeController {

    @Autowired
    private ICoreProjectTypeService iCoreProjectTypeService;

    @ApiOperation("新增")
    @SysLog("新增项目分类")
    @PostMapping("/add")
    public Response add(@RequestBody CoreProjectType coreProjectType) {
        return iCoreProjectTypeService.add(coreProjectType);
    }

    @ApiOperation("编辑")
    @SysLog("编辑项目分类")
    @PutMapping("/update")
    public Response update(@RequestBody CoreProjectType coreProjectType) {
        return iCoreProjectTypeService.update(coreProjectType);
    }

    @ApiOperation("删除")
    @SysLog("删除项目分类")
    @DeleteMapping("/del")
    public Response del(Integer id) {
        iCoreProjectTypeService.removeById(id);
        return Response.ok();
    }

    @ApiOperation("详情")
    @GetMapping("/getOne")
    public Response getOne(Integer id) {
        return Response.ok(iCoreProjectTypeService.getById(id));
    }

    @ApiOperation("列表")
    @GetMapping("/getList")
    public Response getList(String typeName, Integer pageIndex, Integer pageSize) {
        QueryWrapper<CoreProjectType> queryWrapper = new QueryWrapper<>();
        if ((ShiroUtils.getSeeAll().equals(1) ? null : ShiroUtils.getEnterpriseId()) != null) {
            queryWrapper.eq("enterprise_id", ShiroUtils.getEnterpriseId());
        }

        if (StringUtils.isNotBlank(typeName)) {
            queryWrapper.like("type_name", typeName);
        }
        queryWrapper.orderByAsc("type_name");
        Page<CoreProjectType> page = new Page<>(pageIndex, pageSize);
        iCoreProjectTypeService.page(page, queryWrapper);
        return Response.ok(page.getRecords(), page.getTotal());
    }

}

